﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VswService_V7.Utility;
using VswService_V7.DataBase.Tables.L2INTERFACE;

namespace VswService_V7.DataBase.Tables
{
    public partial class SAMPLE_TABLE
    {
        #region field
        public static readonly string tableName = "SAMPLE_TABLE";
        public static readonly string fd_eventNo = "EVENT_NO";
        public static readonly string fd_sampleNo = "SAMPLE_NO";
        public static readonly string fd_taskNo = "TASK_NO";
        public static readonly string fd_batchNo = "BATCH_NO";
        public static readonly string fd_stripNo = "STRIP_NO";
        public static readonly string fd_pipeNo = "PIPE_NO";
        public static readonly string fd_stripSeq = "STRIP_SEQ";
        public static readonly string fd_sampleReason = "SAMPLE_REASON";
        public static readonly string fd_sampleType = "SAMPLE_TYPE";
        public static readonly string fd_dtj = "DTJ";
        public static readonly string fd_sampleLength = "SAMPLE_LENGTH";
        public static readonly string fd_location = "LOCATION";

        public static readonly string fd_fltGap = "FLT_GAP";
        public static readonly string fd_fltTime = "FLT_TIME";
        public static readonly string fd_fltSpeed = "FLT_SPEED";

        public static readonly string fd_sampleResult = "SAMPLE_RESULT";
        public static readonly string fd_updateTime = "UPDATE_TIME";

        #endregion

        /// <summary>
        /// update sample pipe information
        /// </summary>
        /// <param name="samplePipe"></param>
        /// <returns>eventNo</returns>
        public static long UpdateSampleResult(SampleInfo samplePipe)
        {
            try
            {
                string sampleNo = samplePipe.GetSampleNo();
                long eventNo = OracleHelper.GetNextEventNo();
                string sql = string.Format("update {0} set " +
                    "{1}={2}," +
                    "{3}='{4}'," +
                    "{5}='{6}'," +
                    "{7}='{8}'," +
                    "{9}='{10}'," +
                    "{11}='{12}' " +
                    "where {13}='{14}'",
                    tableName,
                    fd_eventNo, eventNo,
                    fd_fltGap, samplePipe.fltGap,
                    fd_fltTime, samplePipe.fltTime,
                    fd_fltSpeed, samplePipe.fltSpeed,
                    fd_sampleResult, samplePipe.sampleResult,
                    fd_updateTime, samplePipe.updateTime,
                    fd_sampleNo, sampleNo);
                string sqlINTFSAMPLE = string.Format("insert into {0}" +
                    "({1},{2}," +
                    "{3},{4},{5},{6},{7}," +
                    "{8},{9},{10},{11},{12}," +
                    "{13})" +
                    "values({14},'{15}'," +
                    "'{16}','{17}','{18}','{19}','{20}'," +
                    "'{21}','{22}','{23}','{24}','{25}'," +
                    "'{26}')",
                     L2INTF_SAMPLE.tableName,
                    fd_eventNo, fd_sampleNo,
                    fd_taskNo, fd_stripNo, fd_batchNo, fd_pipeNo, fd_stripSeq,
                    fd_sampleType, fd_dtj, fd_sampleLength, fd_location,
                    fd_sampleResult,
                    fd_updateTime,
                    eventNo, sampleNo,
                    samplePipe.taskNo, samplePipe.stripNo, samplePipe.batchNo, samplePipe.pipeNo, samplePipe.stripSeq,
                    samplePipe.sampleType, samplePipe.dtj, samplePipe.sampleLength, samplePipe.location,
                    samplePipe.sampleResult,
                    DateTime.Now.ToString(Formats.GetDateFormat()));

                
                string sqlINTFEVENT = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}')",
                    L2INTF_EVENT.tableName, eventNo, L2INTF_EVENT.generateOrUpdateSamplepassport, DateTime.Now.ToString(Formats.GetDateFormat()), "1", "L1", "");
                List<string> list = new List<string>();
                list.Add(sql);
                list.Add(sqlINTFSAMPLE);
                list.Add(sqlINTFEVENT);
                OracleHelper.ExecuteSqlTran(list);
                return eventNo;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("UpdateSampleResult", ex);
                return -1;
                //throw ex;
            }
        }

        /// <summary>
        /// create sample record
        /// </summary>
        /// <param name="pipe"></param>
        /// <returns>eventNo</returns>
        public static long NewSampleRecord(SampleInfo pipe)
        {
            try
            {
                string sampleNo = pipe.GetSampleNo();
                long eventNo = OracleHelper.GetNextEventNo();
                string sql = string.Format("insert into {0}" +
                    "({1},{2}," +
                    "{3},{4},{5},{6},{7}," +
                    "{8},{9},{10},{11},{12}," +
                    "{13},{14},{15}," +
                    "{16}," +
                    "{17})" +
                    "values({18},'{19}'," +
                    "'{20}','{21}','{22}','{23}','{24}'," +
                    "'{25}','{26}','{27}','{28}','{29}'," +
                    "'{30}','{31}','{32}'," +
                    "'{33}'," +
                    "'{34}')",
                    tableName,
                    fd_eventNo, fd_sampleNo,
                    fd_taskNo, fd_stripNo, fd_batchNo, fd_pipeNo, fd_stripSeq,
                    fd_sampleReason, fd_sampleType, fd_dtj, fd_sampleLength, fd_location,
                    fd_fltGap, fd_fltTime, fd_fltSpeed,
                    fd_sampleResult,
                    fd_updateTime,
                    eventNo, sampleNo,
                    pipe.taskNo, pipe.stripNo, pipe.batchNo, pipe.pipeNo, pipe.stripSeq,
                    pipe.sampleReason, pipe.sampleType, pipe.dtj, pipe.sampleLength, pipe.location,
                    pipe.fltGap, pipe.fltTime, pipe.fltSpeed,
                    pipe.sampleResult,
                    DateTime.Now.ToString(Formats.GetDateFormat()));
                string sqlINTFSAMPLE = string.Format("insert into {0}" +
                    "({1},{2}," +
                    "{3},{4},{5},{6},{7}," +
                    "{8},{9},{10},{11},{12}," +
                    "{13})" +
                    "values({14},'{15}'," +
                    "'{16}','{17}','{18}','{19}','{20}'," +
                    "'{21}','{22}','{23}','{24}','{25}'," +
                    "'{26}')",
                     L2INTF_SAMPLE.tableName,
                    fd_eventNo, fd_sampleNo,
                    fd_taskNo, fd_stripNo, fd_batchNo, fd_pipeNo, fd_stripSeq,
                    fd_sampleType, fd_dtj, fd_sampleLength, fd_location,
                    fd_sampleResult,
                    fd_updateTime,
                    eventNo, sampleNo,
                    pipe.taskNo, pipe.stripNo, pipe.batchNo, pipe.pipeNo, pipe.stripSeq,
                    pipe.sampleType, pipe.dtj, pipe.sampleLength, pipe.location,
                    pipe.sampleResult,
                    DateTime.Now.ToString(Formats.GetDateFormat()));
                string sqlINTFEVENT = string.Format("insert into {0} values({1},'{2}','{3}','{4}','{5}','{6}')",
                    L2INTF_EVENT.tableName, eventNo, L2INTF_EVENT.generateOrUpdateSamplepassport, DateTime.Now.ToString(Formats.GetDateFormat()), "1", "L1", "");
                List<string> list = new List<string>();
                list.Add(sql);
                list.Add(sqlINTFSAMPLE);
                list.Add(sqlINTFEVENT);
                OracleHelper.ExecuteSqlTran(list);
                return eventNo;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("NewSampleRecord", ex);
                return -1;
                //throw ex;
            }
        }
    }

    public class SampleInfo
    {
        public SampleInfo(string pipeLocation)
        {
            eventNo = 0;
            sampleSeq = "0";
            taskNo = "0";
            stripNo = "0";
            batchNo = "0";
            pipeNo = "0";
            stripSeq = "0";
            sampleReason = "0";
            sampleType = "-1";
            dtj = "0";
            sampleLength = "0";
            location = pipeLocation;
            fltGap = "0";
            fltTime = "0";
            fltSpeed = "0";
            sampleResult = "0";
            updateTime = "0";
        }

        public long eventNo;
        public string sampleSeq;
        public string taskNo;
        public string stripNo;
        public string batchNo;
        public string pipeNo;
        public string stripSeq;
        public string sampleReason;
        public string sampleType;
        public string dtj;
        public string sampleLength;
        public string location;
        public string fltGap;
        public string fltTime;
        public string fltSpeed;
        public string sampleResult;
        public string updateTime;

        public string GetFullPipeNo()
        {
            if (pipeNo.Length > 3)
                return pipeNo;
            else
            {
                Int64 pipeNum = Int64.Parse(pipeNo);
                string fullPipeNumber = batchNo + pipeNum.ToString("000");
                return fullPipeNumber;
            }
        }

        public string GetSampleNo()
        {
            string sampleNum = GetFullPipeNo() + "-" + sampleType + sampleSeq;
            return sampleNum;
        }

    }
}
